﻿using System.Collections;
using LunchList.Infrastructure;
using LunchList.ViewModels;
using LunchListCore.Interfaces.Agents;
using LunchListCore.Interfaces.Models;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LunchList.Controllers
{
    public class EventsController : LunchListControlerBase
    {
        [Dependency]
        public ILunchAgent LunchAgent { get; set; }

        [Dependency]
        public IGuestContext GuestContext { get; set; }


        //
        // GET: /Events/
        public ActionResult Index()
        {
            EventsViewModel model = new EventsViewModel();

	        model.EventDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 1, 12, 0, 0);

			IList<IEvent> allEvents = LunchAgent.GetEvents() ?? new List<IEvent>();

            model = new EventsViewModel()
            {
				EventListPast = allEvents.Where(x => x.EventDate < DateTime.Now).ToList(),
				EventListFuture = allEvents.Where(x => x.EventDate >= DateTime.Now).ToList(),
                EventDate = model.EventDate,
                UserId = GuestContext.User.AdUserId,
                UserName = GuestContext.User.Name,
            };

            return LunchListView(model);
        }

        public ActionResult GetEventList()
        {
            EventsViewModel model = new EventsViewModel();


			model.EventDate = DateTime.Now;

			IList<IEvent> allEvents = LunchAgent.GetEvents() ?? new List<IEvent>();
            try
            {
                model = new EventsViewModel()
                {
					EventListPast = allEvents.Where(x => x.EventDate < DateTime.Now).ToList(),
					EventListFuture = allEvents.Where(x => x.EventDate >= DateTime.Now).ToList(),
                    EventDate = DateTime.Now,
                    UserId = GuestContext.User.AdUserId,
                    UserName = GuestContext.User.Name,
                };
            }
            catch (Exception ex)
            {
                ErrorController errorController = new ErrorController();
                List<string> errors = new List<string>();
                errors.Add(ex.Message);

                ErrorViewModel errorModel = new ErrorViewModel
                {
                    Errors = errors
                };
                return errorController.DisplayErrorView(errorModel);
            }

            return PartialView("_EventList", model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddEvent(EventsViewModel model)
        {
            if (ModelState.IsValid)
            {
            LunchAgent.AddEvent(model.EventName
                                , model.EventDate
                                , GuestContext.User
                                , GuestContext.User.AdUserId.ToString());

            }
			
			IList<IEvent> allEvents = LunchAgent.GetEvents() ?? new List<IEvent>();
	        model.EventListPast = allEvents.Where(x => x.EventDate < DateTime.Now).ToList();
	        model.EventListFuture = allEvents.Where(x => x.EventDate >= DateTime.Now).ToList();
           
            return PartialView("_EventList", model);
                
        }

        
        public ActionResult DeleteEvent(int id, EventsViewModel model)
        {
            bool result = LunchAgent.DeleteEvent(id);

            if (!result)
            {
                ModelState.AddModelError("", "Unable to delete event.");
            //    EventsViewModel evm = new EventsViewModel();
            //    evm.EventList = 
        }

            return RedirectToAction("Index");

        }

	}
}